package factory_pattern.factory_method.factory;

import factory_pattern.factory_method.ConnectionFactory;
import factory_pattern.factory_method.NetworkConnection;
import factory_pattern.factory_method.conn_impl.UdpConnection;

// UDP连接工厂（具体工厂）
public class UdpFactory implements ConnectionFactory {
    @Override
    public NetworkConnection createConnection(String... params) {
        if (params.length != 2) {
            throw new IllegalArgumentException("UDP工厂需要host和port两个参数");
        }
        try {
            return new UdpConnection(params[0], Integer.parseInt(params[1]));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("端口必须是数字");
        }
    }
}
